package com.loren.demo15.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Service
public class TaskService {

    private static final Random RANDOM = new Random();

    @Async("taskExecutor")
    public CompletableFuture<String> doTaskOne() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread.sleep(RANDOM.nextInt(10000));
        long end = System.currentTimeMillis();
        log.info("task1 complete, time = {}", (end - start));
        return CompletableFuture.completedFuture("task1 complete");
    }

    @Async("taskExecutor")
    public CompletableFuture<String> doTaskTwo() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread.sleep(RANDOM.nextInt(10000));
        long end = System.currentTimeMillis();
        log.info("task2 complete, time = {}", (end - start));
        return CompletableFuture.completedFuture("task2 complete");
    }

    @Async("taskExecutor")
    public CompletableFuture<String> doTaskThree() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread.sleep(RANDOM.nextInt(10000));
        long end = System.currentTimeMillis();
        log.info("task3 complete, time = {}", (end - start));
        return CompletableFuture.completedFuture("task3 complete");
    }
}
